package com.ktjy;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class EmailQueueConsumer {
    public static void main(String[] args) throws IOException, TimeoutException {
        //建立连接
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("127.0.0.1");
        factory.setPort(5672);
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/T384");
        //创建连接对象
        Connection connection = factory.newConnection();
        //创建信道对象
        Channel channel = connection.createChannel();

        //定义一个队列
        String queueName="email_queue";
        channel.queueDeclare(queueName,true,false,false,null);

        //接收信息
        channel.basicConsume(queueName,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag,
                                       Envelope envelope,
                                       AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                System.out.println(consumerTag+"收到"+new String(body));
            }
        });

    }
}
